package tree.leetcode_116_medium;

public class Connect {
    public Node connect(Node root){
        if(root == null){
            return null;
        }
        //root用于最后返回
        Node mostLeft = root;
        while(mostLeft.left != null){
            Node head = mostLeft;

            //每一层都要走完
            while(head != null) {
                //Connect1
                head.left.next = head.right;

                //Connect2
                if (head.next != null) {
                    head.right.next = head.next.left;
                }
                head = head.next;
            }
            mostLeft = mostLeft.left;
        }
        return root;
    }
}
